package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.reggie.common.PageDto;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 套餐(Setmeal)表控制层
 *
 * @author shenjiaqi
 * @since 2024-02-06 00:30:51
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    /**
     * 服务对象
     */
    @Resource
    private SetmealService setmealService;

    /**
     * 套餐新增
     *
     * @param dto
     * @return
     */
    @PostMapping
    public R add(@RequestBody SetmealDto dto) {
        return setmealService.add(dto);
    }

    /**
     * 套餐分页模糊查询
     *
     * @param dto
     * @return
     */
    @GetMapping("/page")
    public R page(PageDto dto) {
        return setmealService.pageList(dto);
    }


    /**
     * 单个套餐回显
     *
     * @param
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        return setmealService.getWithId(id);
    }

    /**
     * 套餐修改
     *
     * @param dto
     * @return
     */
    @PutMapping()
    public R update(@RequestBody SetmealDto dto) {
        return setmealService.updateWithEntity(dto);
    }

    /**
     * 批量上下架
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status, @RequestParam List<String> ids) {
        return setmealService.updateStatus(status, ids);
    }

    /**
     * app套餐列表
     *
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam(required = false) Long categoryId, @RequestParam(required = false) Integer status) {

        List<Setmeal> setmealList = setmealService.list(Wrappers.<Setmeal>lambdaQuery()
                .eq(null != categoryId, Setmeal::getCategoryId, categoryId)
                .eq(null != status, Setmeal::getStatus, status)
                .eq(Setmeal::getIsDeleted, 0)
                .orderByDesc(Setmeal::getUpdateTime));
        return R.success(setmealList);
    }
}

